package com.yzt.message.widget.chatinput.widget;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.yzt.message.R;
import com.yzt.p_base.utils.DisplayUtils;
import com.yzt.plugin_imageloader.ImageLoadUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * author : Irene
 * e-mail : 821970751@qq.com
 * date   : 2020/5/19 16:45
 * desc   :
 */
public class EmojiItemAdapter extends RecyclerView.Adapter<EmojiItemAdapter.ItemViewHolder> {

    private Activity activity;
    private List<AndroidEmoji1.EmojiInfo> list;
    private OnClickEmojiSelectListener listener;
    public int column;

    public EmojiItemAdapter(Activity activity, int column) {
        this.activity = activity;
        this.column = column;
        this.list = new ArrayList<>();
    }

    public void setOnClickEmojiSelectListener(OnClickEmojiSelectListener listener) {
        this.listener = listener;
    }

    public void initData(List<AndroidEmoji1.EmojiInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ItemViewHolder(LayoutInflater.from(activity).inflate(R.layout.item_emoji_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, int position) {
        final AndroidEmoji1.EmojiInfo emojiBean = list.get(position);
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) itemViewHolder.mSelectLayout.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        linearParams.width = DisplayUtils.getDisplayPxWidth() / column;

        LinearLayout.LayoutParams imageParams = (LinearLayout.LayoutParams) itemViewHolder.mIconSelect.getLayoutParams();

        ImageLoadUtil.loadIv(itemViewHolder.mIconSelect.getContext(), emojiBean.resId, itemViewHolder.mIconSelect);

        imageParams.width = DisplayUtils.getDisplayPxWidth() / column - DisplayUtils.dp2px(20);
        imageParams.height = DisplayUtils.getDisplayPxWidth() / column - DisplayUtils.dp2px(20);
        itemViewHolder.mNameTv.setVisibility(View.GONE);
        linearParams.height = DisplayUtils.getDisplayPxWidth() / column;
        itemViewHolder.mIconSelect.setLayoutParams(imageParams);

        itemViewHolder.mSelectLayout.setLayoutParams(linearParams);
        itemViewHolder.mSelectLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.selectIcon(emojiBean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout mSelectLayout;
        private ImageView mIconSelect;
        private TextView mNameTv;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            mSelectLayout = itemView.findViewById(R.id.select_layout);
            mIconSelect = itemView.findViewById(R.id.icon_iv);
            mNameTv = itemView.findViewById(R.id.tv_name);
        }
    }

    public interface OnClickEmojiSelectListener {
        void selectIcon(AndroidEmoji1.EmojiInfo emojiBean);
    }
}
